package com.ruoyix.test.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.test.domain.bo.TagSelectTestBo;
import com.ruoyix.test.domain.vo.TagSelectTestVo;
import com.ruoyix.test.domain.TagSelectTest;
import com.ruoyix.test.mapper.TagSelectTestMapper;
import com.ruoyix.test.service.ITagSelectTestService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-13
 */
@RequiredArgsConstructor
@Service
public class TagSelectTestServiceImpl implements ITagSelectTestService {

    private final TagSelectTestMapper baseMapper;

    /**
     * 查询用户信息
     */
    @Override
    public TagSelectTestVo queryById(Long userId){
        return baseMapper.selectVoById(userId);
    }

    /**
     * 查询用户信息列表
     */
    @Override
    public TableDataInfo<TagSelectTestVo> queryPageList(TagSelectTestBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TagSelectTest> lqw = buildQueryWrapper(bo);
        Page<TagSelectTestVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户信息列表
     */
    @Override
    public List<TagSelectTestVo> queryList(TagSelectTestBo bo) {
        LambdaQueryWrapper<TagSelectTest> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TagSelectTest> buildQueryWrapper(TagSelectTestBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TagSelectTest> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, TagSelectTest::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), TagSelectTest::getUserName, bo.getUserName());
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), TagSelectTest::getNickName, bo.getNickName());
        lqw.eq(StringUtils.isNotBlank(bo.getUserType()), TagSelectTest::getUserType, bo.getUserType());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), TagSelectTest::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getPhonenumber()), TagSelectTest::getPhonenumber, bo.getPhonenumber());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), TagSelectTest::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), TagSelectTest::getAvatar, bo.getAvatar());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), TagSelectTest::getPassword, bo.getPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TagSelectTest::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getLoginIp()), TagSelectTest::getLoginIp, bo.getLoginIp());
        lqw.eq(bo.getLoginDate() != null, TagSelectTest::getLoginDate, bo.getLoginDate());
        lqw.in(bo.getNodeIds()!=null && bo.getNodeIds().size()>0 ,TagSelectTest::getDeptId,bo.getNodeIds());
        return lqw;
    }

    /**
     * 新增用户信息
     */
    @Override
    public Boolean insertByBo(TagSelectTestBo bo) {
        TagSelectTest add = BeanUtil.toBean(bo, TagSelectTest.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserId(add.getUserId());
        }
        return flag;
    }

    /**
     * 修改用户信息
     */
    @Override
    public Boolean updateByBo(TagSelectTestBo bo) {
        TagSelectTest update = BeanUtil.toBean(bo, TagSelectTest.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TagSelectTest entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
